#include "MCArcParametricWindow.h"
#include "ui_MCArcParametricWindow.h"
#include <MCMathUtils.h>
#include <qvalidator.h>

MCArcParametricWindow::MCArcParametricWindow(MCCommand *command, MCArcVisual *arcVisual, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MCArcParametricWindow),
    _currentCommand(command),
    _arcVisual(arcVisual),
    _arc(arcVisual->arc())
{
    ui->setupUi(this);

    ui->x1_le->setText(QString::number(_arc.center().x));
    ui->y1_le->setText(QString::number(_arc.center().y));
    ui->radius_le->setText(QString::number(_arc.radius()));
    ui->startAngle_le->setText(QString::number(MCMathUtils::radianToDegree(_arc.startAngle())));
    ui->endAngle_le->setText(QString::number(MCMathUtils::radianToDegree(_arc.endAngle())));

    connect(ui->pushButton, &QPushButton::clicked, this, &MCArcParametricWindow::on_apply_btn_clicked);
    connect(ui->pushButton_2, &QPushButton::clicked, this, &MCArcParametricWindow::on_terminate_btn_clicked);
}

MCArcParametricWindow::~MCArcParametricWindow()
{
    delete ui;
}

void MCArcParametricWindow::on_x1_le_textChanged(const QString &arg1)
{
    if (isTextValid(arg1))
    {
        double x = arg1.toDouble();
        _arc = MCArc2D(MCPoint2D(x, _arc.center().y), _arc.radius(), _arc.startAngle(), _arc.endAngle());
    }

    updateValue();
}


void MCArcParametricWindow::on_y1_le_textChanged(const QString &arg1)
{
    if (isTextValid(arg1))
    {
        double y = arg1.toDouble();
        _arc = MCArc2D(MCPoint2D(_arc.center().x, y), _arc.radius(), _arc.startAngle(), _arc.endAngle());
    }

    updateValue();
}


void MCArcParametricWindow::on_radius_le_textChanged(const QString &arg1)
{
    if (isTextValid(arg1))
    {
        double rad = arg1.toDouble();
        _arc = MCArc2D(_arc.center(), rad, _arc.startAngle(), _arc.endAngle());
    }

    updateValue();
}


void MCArcParametricWindow::on_startAngle_le_textChanged(const QString &arg1)
{
    if (isTextValid(arg1))
    {
        double startAngle = arg1.toDouble();
        startAngle = std::clamp(startAngle, -360., 360.);
        _arc = MCArc2D(_arc.center(), _arc.radius(), MCMathUtils::degreeToRadian(startAngle), _arc.endAngle());
    }

    updateValue();
}


void MCArcParametricWindow::on_endAngle_le_textChanged(const QString &arg1)
{
    if (isTextValid(arg1))
    {
        double endAngle = arg1.toDouble();
        endAngle = std::clamp(endAngle, -360., 360.);
        _arc = MCArc2D(_arc.center(), _arc.radius(), _arc.startAngle(), MCMathUtils::degreeToRadian(endAngle));
    }

    updateValue();
}

void MCArcParametricWindow::on_apply_btn_clicked()
{
    this->close();
    _currentCommand->finalize();
}

void MCArcParametricWindow::on_terminate_btn_clicked()
{
    this->close();
    _currentCommand->terminate();
}

bool MCArcParametricWindow::isTextValid(QString text)
{
    QDoubleValidator validator;
    int pos = 0;
    QValidator::State state = validator.validate(text, pos);

    if (state == QValidator::Acceptable)
    {
        return true;
    }

    return false;
}

void MCArcParametricWindow::updateValue()
{
    _arcVisual->update(_arc);
    _currentCommand->presenter_()->redraw();
}

